package com.example.myfirstapplication

import android.content.ContentValues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.myfirstapplication.data.Student
import com.example.myfirstapplication.dbHelper.MyDatabaseHelper
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import com.example.myfirstapplication.utils.myToast
import kotlinx.android.synthetic.main.activity_database.*

class DatabaseActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_database)
        val dbHelper = MyDatabaseHelper(this, "student.db", 4)
        val db = dbHelper.writableDatabase
        btnCreateDB.setOnClickListener {
            dbHelper.writableDatabase
        }
        btnInsertData.setOnClickListener {
/*            val studentData = ContentValues().apply {
                put("stu_num", "117060100")
                put("stu_name", "张三")
                put("stu_gender", "女")
                put("stu_age", "18")
            }*/
            val studentData = cvOf(
                "stu_num" to "117060100",
                "stu_name" to "张三",
                "stu_gender" to "女",
                "stu_age" to 17
            )
            db.insert("student", null, studentData)
            val scoreData = ContentValues().apply {
                put("stu_num", "117060100")
                put("subject", "计算机")
                put("score", "77")
                put("term", "2021-2022-2")
            }
            db.insert("score", null, scoreData)
            Log.i(Tag, "InsertData succeeded")
        }
        btnUpdateData.setOnClickListener {
            val updateData = ContentValues().apply {
                put("subject", "高等数学")
                put("score", "88")
            }
            val reRows = db.update("score", updateData, "id = ?", arrayOf("2"))
            myToast(this, "更新的行数:${reRows}")
        }
        btnDeleteData.setOnClickListener {
            db.delete("score", "id=?", arrayOf("2"))
        }
        btnQueryData.setOnClickListener {
            var cursor = db.query("student", null, null, null, null, null, null)
            if (cursor.moveToFirst()) {
                do {
                    //val stuId = cursor.getInt(cursor.getColumnIndex("id"))
                    val stuName = cursor.getString(cursor.getColumnIndex("stu_name"))
                    val stuNum = cursor.getString(cursor.getColumnIndex("stu_num"))
                    val stuGender = cursor.getString(cursor.getColumnIndex("stu_gender"))
                    val stuAge = cursor.getInt(cursor.getColumnIndex("stu_age"))
                    val stuObj = Student(
                        stuNum, stuName, stuGender, stuAge
                    ) //stuId,
                    Log.i(Tag, "student:${stuObj}")
                } while (cursor.moveToNext())
            }
        }
    }
}